package view.visualizations;

import java.util.Date;
import java.util.Set;

import prefuse.Constants;
import prefuse.action.ActionList;
import prefuse.action.RepaintAction;
import prefuse.action.assignment.DataColorAction;
import prefuse.action.assignment.DataShapeAction;
import prefuse.action.layout.AxisLabelLayout;
import prefuse.action.layout.AxisLayout;
import prefuse.data.expression.ColumnExpression;
import prefuse.data.expression.ComparisonPredicate;
import prefuse.data.expression.ObjectLiteral;
import prefuse.data.expression.Predicate;
import prefuse.render.AbstractShapeRenderer;
import prefuse.render.AxisRenderer;
import prefuse.render.Renderer;
import prefuse.render.RendererFactory;
import prefuse.render.ShapeRenderer;
import prefuse.visual.VisualItem;
import prefuse.visual.VisualTable;
import prefuse.visual.expression.VisiblePredicate;
import view.ViewManager;
import view.renderers.PointRenderer;
import view.renderers.TeamColorAction;
import data.DataManager;


public class DateZoomVis extends XCVis {

	private Date date;
	
	public DateZoomVis(final Date date) {
		super();
		this.date = date;
		setupVis();
	}
	
	public Date getDate() {
		return date;
	}
	
	@Override
	public void setupVis() {
		this.removeGroup("data");
		Predicate p = new ComparisonPredicate(ComparisonPredicate.EQ, new ColumnExpression("Date"), new ObjectLiteral(date));
		VisualTable table = this.addTable("data", DataManager.getInstance().getTable(), p);
		
		Set<Object> meets = DataManager.getInstance().getUniqueColumnEntries("Meet", table);
		if (meets.size() == 1) {
			this.canceled = true;
			ViewManager.setCurrentVis(new MeetZoomVis(date, (String) meets.iterator().next()));
			return;
		}
		
		AxisLayout x_axis = new AxisLayout("data", "Meet", Constants.X_AXIS,
				VisiblePredicate.TRUE);
 
		AxisLayout y_axis = new AxisLayout("data", "Time", Constants.Y_AXIS,
				VisiblePredicate.TRUE);
		
		AxisLabelLayout x_labels = new AxisLabelLayout("xlab", x_axis);
		AxisLabelLayout y_labels = new AxisLabelLayout("ylab", y_axis);
		
		
		TeamColorAction color = new TeamColorAction("data", "School", Constants.NOMINAL, 
				VisualItem.STROKECOLOR, this);
		
		ActionList draw = new ActionList();
		draw.add(x_axis);
		draw.add(y_axis);
		draw.add(x_labels);
		draw.add(y_labels);
		draw.add(color);
		
//		String[] ttNames = {"Name", "School"};
//		ToolTipControl ttControl = new ToolTipControl(ttNames);
//		display.addControlListener(ttControl);
		
		this.setRendererFactory(new RendererFactory() {
			PointRenderer pr = new PointRenderer(DateZoomVis.this);
			Renderer arY = new AxisRenderer(Constants.FAR_LEFT,
					Constants.CENTER);
			Renderer arX = new AxisRenderer(Constants.CENTER,
					Constants.FAR_BOTTOM);
 
			public Renderer getRenderer(VisualItem item) {
				
				return item.isInGroup("ylab") ? arY
						: item.isInGroup("xlab") ? arX : pr;
			}
		});
		
		DataShapeAction shape = new DataShapeAction("data", "Race");
		draw.add(shape);
		draw.add(new RepaintAction());
		this.putAction("draw", draw);
		this.run("draw");

		ActionList update = new ActionList();
		update.add(x_axis);
		update.add(y_axis);
		update.add(x_labels);
		update.add(y_labels);
		update.add(new RepaintAction());
		this.putAction("update", update);

		this.repaint();
	}
}
